package cs422.group1.projectsquirrel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cs422.group1.projectsquirrel.ExtraSurvey.nextQuestionListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class ExtraSurvey7 extends Activity
{
	//Instance variables.
	private Button nextButton;
	private EditText comments, email;
	private Pattern pattern;
	private Matcher matcher;
	private static final String EMAIL_PATTERN = 
        "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	public void onCreate(Bundle savedInstanceState)
	{
		
		pattern = Pattern.compile(EMAIL_PATTERN);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.extrasurveypage7);
		
		nextButton = (Button)findViewById(R.id.nextButton7);
		comments = (EditText)findViewById(R.id.comments);
		email = (EditText)findViewById(R.id.txtEmailAddress);
        //Attaching a listener for callback on the next button.
        nextButton.setOnClickListener(new nextQuestionListener()); 
        
        if (ObservationDataManager.isEdit())
        {
        	email.setText(ObservationDataManager.getData().emailAddress);
        	comments.setText(ObservationDataManager.getData().comments);
        }
        
	}
	
	
	//Callback function for the NEXT button.
	public class nextQuestionListener implements OnClickListener
	{
		public void onClick(View view)
		{
			
			matcher = pattern.matcher(email.getText().toString());
			if (!matcher.matches())
			{
				Toast.makeText(ProjectSquirrel.myAppContext, "Please enter a valid email address", Toast.LENGTH_LONG).show();
				return;
			}
			
			ObservationDataManager.getData().comments = comments.getText().toString();
			ObservationDataManager.getData().emailAddress = email.getText().toString();
			ObservationDataManager.getData().longSurvey = true;
			ObservationDataManager.saveData(ObservationDataManager.getData());
			nextPage();
		}
	}
		
	
	//Done with this activity, and moving on to the next one.
	private void nextPage()
	{
		Toast.makeText(this, "Long survey succesfully submitted", Toast.LENGTH_LONG).show();
		Intent intent = new Intent(this.getBaseContext(),ProjectSquirrel.class);
		startActivity(intent);
	}

	
}
